package patten.xingwei.strategy;

import com.sun.xml.internal.ws.api.message.ExceptionHasMessage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Description：搜索策略上下文
 * Author：zwenzhi
 * Date：2023/11/16 15:38
 */
public class SearchStrategyContext {
    private Map<String ,SearchStrategy> searchMap ;

    public SearchStrategyContext(List<SearchStrategy> searchStrategyList) throws Exception {
        if(searchStrategyList == null || searchStrategyList.size()==0){
            throw new Exception("有问题");
        }
        Map<String, SearchStrategy> strategyMap = searchStrategyList.stream()
                .collect(Collectors.toMap(SearchStrategy::getType, Function.identity()));
        this.searchMap = strategyMap;
    }

    public void search(String type , String message) throws Exception {
        SearchStrategy searchStrategy = searchMap.get(type);
        if(searchStrategy== null){
            throw new Exception("没有对应的策略");
        }
        searchStrategy.search(message);
    }

}
